package com.zimin.cloud.mine.feign.config;

import cn.hutool.core.util.StrUtil;
import com.zimin.common.constant.ServletConstant;
import com.zimin.common.utils.ServletUtil;
import feign.Request;
import feign.RequestInterceptor;
import feign.RequestTemplate;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @author zimin
 * @since 2022/3/23 3:21 下午
 */
@Slf4j
@Configuration
public class FeignConfig implements RequestInterceptor {

    @Override
    public void apply(RequestTemplate requestTemplate) {
        HttpServletRequest httpServletRequest = null;
        try {
            httpServletRequest = ServletUtil.getRequest();
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (httpServletRequest != null) {
            Map<String, String> headers = ServletUtil.getHeaders();
            String userId = headers.get(ServletConstant.USER_ID);
            if (StrUtil.isNotEmpty(userId)) {
                requestTemplate.header(ServletConstant.USER_ID, userId);
            }
            String userMail = headers.get(ServletConstant.USER_MAIL);
            if (StrUtil.isNotEmpty(userMail)) {
                requestTemplate.header(ServletConstant.USER_MAIL, userMail);
            }
            String userName = headers.get(ServletConstant.USER_NAME);
            if (StrUtil.isNotEmpty(userName)) {
                requestTemplate.header(ServletConstant.USER_NAME, userName);
            }
        }
    }

    /**
     * <p>feign请求超时设置</p>
     *
     * @param env 配置文件
     * @return feign.Request.Options
     * @since 2022-03-23 15:34:13
     */
    @Bean
    Request.Options requestOptions(ConfigurableEnvironment env) {
        //http建立socket超时时间
        Integer ribbonConnectTimeout = env.getProperty("ribbon.ConnectTimeout", int.class, 6000);
        //http读取响应socket超时时间
        Integer ribbonReadTimeout = env.getProperty("ribbon.ReadTimeout", int.class, 6000);
        return new Request.Options(ribbonConnectTimeout, ribbonReadTimeout);
    }

    /**
     * <p>ribbon针对Http的readTimeout和connectTimeCount直接用配置 ribbon.readTimeout和ribbon.ConnectTimeout是无效的</p>
     *
     * @param
     * @return org.springframework.web.client.RestTemplate
     * @since 2022-03-23 15:31:54
     */
    @Bean
    @LoadBalanced
    public RestTemplate restTemplate() {
        HttpComponentsClientHttpRequestFactory httpRequestFactory = new HttpComponentsClientHttpRequestFactory();
        httpRequestFactory.setReadTimeout(5000);
        httpRequestFactory.setConnectTimeout(5000);
        return new RestTemplate(httpRequestFactory);
    }
}
